package com.shanghaionstar.login.register;

import android.content.Intent;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.AppScheduler;
import com.onstar.cn.framework.utils.ExceptionUtil;
import com.onstar.cn.mag.joinme.RegisterToVisitorResponse;
import com.shanghaionstar.activity.MainTabActivity;
import com.shanghaionstar.login.LoginActivity;
import com.shanghaionstar.login.register.service.JoinMeService;

import rx.Subscriber;
import rx.Subscription;

/**
 * Created by 7828wc on 16/7/8.
 */
public class JoinMeUtils {
    private static Subscription otherSubscription;

    /**
     * Check account is have permission upgrade,need imvpView;
     * imvpView is null,throw java.lang.nullPointException
     * @param imvpView
     */
    public static void checkVisitor(final IMVPView imvpView) {
        if (imvpView == null) {
            throw new NullPointerException("imvpView is null");
        }
        AppScheduler mvpScheduler = new AppScheduler();
        JoinMeService joinMeService = new JoinMeService(imvpView.onActivity());
//        BusinessUtil.sendReportByClickType(imvpView.onActivity(), FunctionNameFindId.Upgrade_Subscriber.getStringValue());
        otherSubscription = joinMeService.visitorCheckAndUpgrade(null, "", "", true).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Subscriber<RegisterToVisitorResponse>() {
                    @Override
                    public void onCompleted() {
                        imvpView.setLoading(false);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        imvpView.setLoading(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        finish();
                        imvpView.setLoading(false);
                        if (e instanceof OnStarException) {
                            imvpView.onResponseError((OnStarException) e);
                        } else {
                            imvpView.onResponseError(ExceptionUtil.defaultException(new Exception(e.getMessage())));
                        }
                    }

                    @Override
                    public void onNext(RegisterToVisitorResponse registerResponseEntity) {
                        if(imvpView.onActivity() instanceof MainTabActivity) {
                            finish();
                        }
                        imvpView.setLoading(false);
                        Intent intent = new Intent(imvpView.onActivity(), LoginActivity.class);
                        intent.putExtra("isUpgradeToSub", true);
                        imvpView.onActivity().startActivityForResult(intent, Constants.REQ_CODE_JOINME);
                    }
                });
    }

    private static void finish() {
        if (otherSubscription != null && !otherSubscription.isUnsubscribed())
            otherSubscription.unsubscribe();
    }
}
